//
//  HCConsole.m
//  HCToolBox
//
//  Created by Hotacool on 2019/3/14.
//

#import "HCConsole.h"
#import "UIView+HCTB.h"
#import "HCConsoleVcr.h"

#define kHCConsoleLogShowLine @"kHCConsoleLogShowLine"

@interface HCConsole ()
@property(nonatomic, strong) UIWindow *consoleWindow;
@property(nonatomic, strong) HCConsoleVcr *consoleVcr;

@end

@implementation HCConsole

+ (instancetype)sharedInstance {
    static dispatch_once_t onceToken;
    static HCConsole *instance;
    dispatch_once(&onceToken, ^{
        instance = [[super allocWithZone:NULL] init];
        instance.canShow = YES;
    });
    return instance;
}

+ (void)show {
    HCConsole *console = [HCConsole sharedInstance];
    if (console.canShow) {
        console.consoleWindow.hidden = NO;
        [[NSNotificationCenter defaultCenter] addObserver:console selector:@selector(windowBecomKeyWindowNotific:) name:UIWindowDidBecomeKeyNotification object:nil];
    }
}

+ (void)hide {
    [[NSNotificationCenter defaultCenter] removeObserver:[HCConsole sharedInstance]];
    [HCConsole sharedInstance].consoleWindow.hidden = YES;
    [HCConsole sharedInstance].consoleWindow = nil;
}

+ (NSInteger)getLogShowLine {
    NSInteger showLine = 200;
    NSNumber *localShowLine = [[NSUserDefaults standardUserDefaults] objectForKey:kHCConsoleLogShowLine];
    if (localShowLine) {
        showLine = [localShowLine integerValue];
    }
    return showLine;
}

+ (void)setLogShowLine:(NSInteger)showLine {
    if (showLine > 0) {
        [[NSUserDefaults standardUserDefaults] setObject:@(showLine) forKey:kHCConsoleLogShowLine];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}

- (UIWindow *)consoleWindow {
    if (!_consoleWindow) {
        _consoleWindow = [[UIWindow alloc] init];
        @weakIt(self);
        _consoleWindow.hctb_hitTestBlock = ^__kindof UIView * _Nonnull(CGPoint point, UIEvent * _Nonnull event, __kindof UIView * _Nonnull originalView) {
            @strongIt(self);
            return originalView == self.consoleWindow ? nil : originalView;
        };
        _consoleWindow.backgroundColor = nil;
        _consoleWindow.rootViewController = self.consoleVcr;
        UIWindow *topWindow = [UIApplication sharedApplication].windows.lastObject;
        [_consoleWindow setWindowLevel:topWindow.windowLevel + 1];
    }
    return _consoleWindow;
}

- (BOOL)isShow {
    return !self.consoleWindow.hidden;
}

- (HCConsoleVcr *)consoleVcr{
    if (!_consoleVcr) {
        _consoleVcr = [[HCConsoleVcr alloc] init];
        @weakIt(self);
        _consoleVcr.statusChangeBlock = ^(BOOL isFirstResponder) {
            @strongIt(self);
            if (!isFirstResponder) {
                [self windowResignKeyWindow];
            }
        };
    }
    return _consoleVcr;
}

- (void)windowResignKeyWindow{
    id<UIApplicationDelegate> delegate = [UIApplication sharedApplication].delegate;
    if ([delegate respondsToSelector:@selector(window)]) {
        [delegate.window makeKeyWindow];
    }
}

#pragma mark notific
- (void)windowBecomKeyWindowNotific:(NSNotification *)noti {
    if ([HCConsole sharedInstance].consoleWindow.isKeyWindow) {
        if (![self.consoleVcr searchFieldIsFirstResponse]) {
            [self windowResignKeyWindow];
        }
    }
}

@end
